package com.common.utils;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Arrays;
import java.util.List;

/**
 * 日志生成器
 * 用于生成日志信息
 * 
 * @author liujun
 * @date 2024-09-21
 */
@Setter
@Getter
@NoArgsConstructor
public class LogCreator {

    /**
     * 日志级别DEBUG
     */
    public static final String LOG_LEVEL_DEBUG = "debug";

    /**
     * 日志级别INFO
     */
    public static final String LOG_LEVEL_INFO = "info";

    /**
     * 日志级别ERROR
     */
    public static final String LOG_LEVEL_ERROR = "error";

    /**
     * 日志级别NONE
     */
    public static final String LOG_LEVEL_NONE = "none";

    /**
     * 当前日志级别
     * 可以更改为其他级别，来控制日志输出
     */
    private String logLevelDebug = "debug";

    /**
     *
     * 日志级别排序
     */
    private static final List<String> LOG_LEVEL_SORT = Arrays.asList(LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_ERROR, LOG_LEVEL_NONE);
    
    public LogCreator(String logLevel) {
        if (LOG_LEVEL_SORT.contains(logLevel)) {
            logLevelDebug = logLevel;
        } else {
            logLevelDebug = LOG_LEVEL_DEBUG;
            print(LOG_LEVEL_ERROR, "日志级别设置错误，已设置为默认级别DEBUG");
        }
    }

    public void print(String logLevel, String message) {
        if (logLevelDebug.equals(LOG_LEVEL_NONE)) {
            return;
        }
        if (LOG_LEVEL_SORT.indexOf(logLevel) >= LOG_LEVEL_SORT.indexOf(logLevelDebug)) {
            //可以更改日志输出方式来支持其他日志框架
            System.out.printf("[%s] %s%n", logLevel, message);
        }
    }

}
